﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Orders;
using LuxDiner.Logic.Orders;
namespace LuxDiner.Web.Management.Areas.Orders.Controllers
{
    [RouteArea("Orders")]
    [RoutePrefix("Repertory")]
    public class RepertoryApiController : ApiController
    {
        [Route("GetTops")]
        [HttpPost]
        public JsonEntitySet<RepertoryModel> GetTops(JObject request)
        {
            string errinfo = "no data";
            try
            {
                RepertoryQuery sea = new RepertoryQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.ProductID = request.Value<int>("productID");
                sea.HouseID = request.Value<int>("houseID");
                sea.GetProduct = request.Value<bool>("getProduct");
                sea.GetHouse = request.Value<bool>("getHouse");
                sea.HideZero = request.Value<bool>("hideZero");
                RepertoryCollection result;
                int repType = request.Value<int>("repType");
                switch (repType)
                {
                    case 2:
                        result = RepertoryLogic.GetRepertoriesByHouse(sea);
                        break;
                    case 3:
                        result = RepertoryLogic.GetRepertoriesByRetention(sea);
                        break;
                    default:
                        result = RepertoryLogic.GetRepertories(sea);
                        break;
                }
                return new JsonEntitySet<RepertoryModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<RepertoryModel>(null, errinfo, false);
        }
        [Route("GetDetails")]
        [HttpPost]
        public JsonEntitySet<RepertoryModel> GetDetails(JObject request)
        {
            string errinfo = "no data";
            try
            {
                RepertoryQuery sea = new RepertoryQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.ProductID = request.Value<int>("productID");
                sea.HouseID = request.Value<int>("houseID");
                sea.GetProduct = request.Value<bool>("getProduct");
                sea.GetHouse = request.Value<bool>("getHouse");
                sea.Retention = request.Value<string>("retention");
                var result = RepertoryLogic.GetDetails(sea);
                return new JsonEntitySet<RepertoryModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<RepertoryModel>(null, errinfo, false);
        }
    }
}
